package com.hzhr.iot.cqgas.ds.controller;

import com.hzhr.iot.cqgas.ds.dao.user.example.UsrUserDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrUserInRoleDao;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserExample;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserInRoleEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrUserInRoleExample;
import com.hzhr.iot.cqgas.ds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UsrUserDao usrUserDao;
    @Autowired
    UsrUserInRoleDao usrUserInRoleDao;

    @RequestMapping(value = "/syncUser")
    public void syncIotCompany() {
        UsrUserEntity usrUserEntity = new UsrUserEntity();
        usrUserEntity.setState("3");
        UsrUserExample usrUserExample = new UsrUserExample();
        usrUserExample.createCriteria().andOutIdIsNotNull();
        usrUserDao.updateByExampleSelective(usrUserEntity, usrUserExample);

        userService.syncUser();

        usrUserExample = new UsrUserExample();
        usrUserExample.createCriteria().andStateEqualTo("3");
        usrUserDao.deleteByExample(usrUserExample);
    }

    @RequestMapping({"/syncUserInRole"})
    public void syncUserInRole() {
        UsrUserExample usrUserExample = new UsrUserExample();
        usrUserExample.createCriteria().andOutIdIsNotNull();
        List<UsrUserEntity> usrUserEntities = this.usrUserDao.selectByExample(usrUserExample);

        Iterator iterator = usrUserEntities.iterator();
        while (iterator.hasNext()) {
            UsrUserEntity usrUserEntity = (UsrUserEntity)iterator.next();
            UsrUserInRoleExample example = new UsrUserInRoleExample();
            example.createCriteria().andUserIdEqualTo(usrUserEntity.getId());
            List<UsrUserInRoleEntity> usrUserInRoleEntities = this.usrUserInRoleDao.selectByExample(example);
            if (usrUserInRoleEntities.size() == 0) {
                UsrUserInRoleEntity usrUserInRoleEntity = new UsrUserInRoleEntity();
                usrUserInRoleEntity.setUserId(usrUserEntity.getId());
                usrUserInRoleEntity.setRoleId(Integer.valueOf(2));
                this.usrUserInRoleDao.insert(usrUserInRoleEntity);
            }
        }

    }
}
